<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class sys_region_manage extends CI_Controller {


	public function __construct() {
		session_start();
		parent::__construct();
	}


	public function index(){

		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();
		
		$this->load->library("common/right_manage");
		$data['update_region']	=false;
		if ($this->right_manage->check_right("update_region")) {
			$data['update_region']	=true;
		}
		$data['delete_region']	=false;
		if ($this->right_manage->check_right("delete_region")) {
			$data['delete_region']=true;
		}
		$data['add_region']	=false;
		if ($this->right_manage->check_right("add_region")) {
			$data['add_region']	=true;
		}
		$data['query_region']	=false;
		if ($this->right_manage->check_right("query_region")) {
			$data['query_region']	=true;
		}
		
		

		$this->load->library("sys/region");
		$region_id				= intval($this->input->get('region_id'));
		$data['top_regions']	= $this->region->get_regions(0);

		$data['regions']= null;
		$data['top_region_id']  =0;
		$data['second_regions']  =null;
		$data['second_region_id']  =0;
		$data['has_link']=true;

		if ($region_id==null) {
			$data['top_regions']		= $this->region->get_regions(0);
			$data['top_region_id']		=0;
			$data['regions']			=$data['top_regions'];
		}else{
			$parent_id= $this->region->get_parent_id($region_id);
			if ($parent_id==0) {
				
				$data['top_region_id']	=$region_id;
				$data['second_regions'] =$this->region->get_regions($region_id);
				$data['regions']		=$data['second_regions'];
			}else{
				$data['has_link']=false;
				$data['top_region_id'] =$parent_id;
				$data['second_region_id']  =$region_id;
				$data['second_regions']  =$this->region->get_regions($parent_id);
				$data['regions']		=$this->region->get_regions($region_id);
			}
		}
		$data['region_id']=$region_id;
		$this->load->view("sys/region/region_main",$data);

	}

	public function add_region(){

		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();
		
		
		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("add_region",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}

		$this->load->library("sys/region");
		$data['top_regions']	= $this->region->get_regions(0);


		$this->load->view("sys/region/add_region",$data);

	}

	public function save_region(){
		$this->load->library("common/login_manage");
		if ($this->login_manage->check_login()==false) {
			$data['result']		=false;
			$data['message']	='请您先登录';
			echo json_encode($data);die();
		}
		
		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("add_region",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			echo json_encode($data);die();
		}
		$region['region_name'] =trim($this->input->get_post("region_name"));
		$region['region_type'] =$this->input->get_post("region_type");
		$region['parent_id'] =$this->input->get_post("parent2");
		if ($region['parent_id']=="") {
			$region['parent_id']=$this->input->get_post("parent1");
			$region['region_type']="";
		}
		$this->load->library("sys/region");
		$this->load->helper("common");
		$result =$this->region->save_region($region);
		echo JSON($result);
	}

	public function get_regions(){
		$this->load->helper("common");
		$this->load->library("sys/region");
		$region_id=$this->input->get('region_id');
		if ($region_id==null) {
			$region_id=0;
		}
		$regions=$this->region->get_regions($region_id);

		echo JSON($regions);
	}

	public function delete_region(){

		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();
		
		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("delete_region",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}

		$this->load->library("sys/region");
		$region_id	= intval($this->input->get('id'));
		$this->region->set_delete_region($region_id);
		echo "<script>history.go(-1)</script>";
	}

	public function edit_region(){

		$this->load->library("common/login_manage");
		$this->login_manage->redirect_url_check_login();

		$this->load->library("common/user_menu");
		$data['user_menu']=$this->user_menu->get_user_menu();
		
		
		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("update_region",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}

		$region_id =$this->input->get('id');

		$this->load->library("sys/region");
		$data['top_regions']	= $this->region->get_regions(0);
		$data['top_region_id']  =0;
		$data['second_regions']  =null;
		$data['second_region_id']  =0;
		$data['be_last_region']=false;
		$region_info= $this->region->get_region_info($region_id);

		//第一级 省
		if ($region_info['parent_id']==0) {
			$data['top_region_id']	=0;
		}else{
			//第二级 市
			$parent_p_id =$this->region->get_parent_id($region_info['parent_id']);
			if ($parent_p_id==-1) {
				$data['top_region_id'] 		=$region_info['parent_id'];
				$data['second_regions']  	=$this->region->get_regions($region_info['parent_id']);
				$data['second_region_id']  	=0;
				//第三级 区
			}else{
				$data['second_regions']  	=$this->region->get_regions($parent_p_id);
				$data['top_region_id'] 		=$parent_p_id;
				$data['second_region_id']  	=$region_info['parent_id'];
				$data['be_last_region']		=true;
			}
		}
		$data['region_info']=$region_info;
		$this->load->view("sys/region/edit_region",$data);
	}

	public function update_region(){
		$this->load->library("common/login_manage");
		if ($this->login_manage->check_login()==false) {
			$data['result']		=false;
			$data['message']	='请您先登录';
			echo json_encode($data);die();
		}
		
		$this->load->library("common/right_manage");
		if (!$this->right_manage->check_right("update_region",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			echo json_encode($data);die();
		}
		$region['region_name'] =trim($this->input->get_post("region_name"));
		$region['region_type'] =$this->input->get_post("region_type");
		$region['parent_id'] =$this->input->get_post("parent2");
		if ($region['parent_id']=="") {
			$region['parent_id']=$this->input->get_post("parent1");
			$region['region_type']="";
		}
		$region_id =$this->input->get_post('region_id');
		$this->load->library("sys/region");
		$this->load->helper("common");
		$result =$this->region->update_region($region,$region_id);
		echo JSON($result);
	}
	
	public function get_api_regions(){
		$this->load->helper("common");
		$this->load->library("sys/region");
		$region_id=$this->input->get('region_id');
		if ($region_id==null) {
			$region_id=0;
		}
		
		$result['ret_code']=0;
		$result['err_code']=0;
		$regions=$this->region->get_regions($region_id);
		$result['data']=$regions;
	
		echo JSON($result);
	}

}